<?php 
if (! $_COOKIE["Cookie"]){?>
	<div id="content" style="width: 650px;">
	<h2><?=t("Zum Login werden Cookies benötigt.")?></h2>
	<h3><?=t("Bitte aktivieren Sie diese Funktion in Ihrem Browser.")?></h3>

	<form>
		<input type=submit value="<?=t("Test wiederholen")?>" />
	</form>
<?php }else{?>
	<div id="subnavi">
		<h2><?=t("Login über:")?></h2>
		<ul>
			<li class="<?=($_GET["mod"] == "twitter")?"select":"norm"?>"><a href="auth-twitter.html">Twitter</a></li>
			<li class="<?=($_GET["mod"] == "facebook")?"select":"norm"?>"><a href="auth-facebook.html">Facebook</a></li>
			<li class="<?=($_GET["mod"] == "mail")?"select":"norm"?>"><a href="auth-mail.html">eMail</a></li>
		</ul>
	</div>
	<div id="content"><?php 
	if ($_SESSION["auth"]){
		if ($_GET["logout"]){
			unset($_SESSION["auth"]);			// Login Algemein
			unset($_SESSION["access_token"]);	// Login Twitter
			$here = tmhUtilities::php_self();
			header("Location: {$here}");
			die('');
		}else{?>
			<h1><?=t("Du bist bereits eingeloggt")?></h1><br />
			<?=t("als")?> <big><?=$_SESSION["auth"]["name"]?></big> <?=t("über")?> <?=$_SESSION["auth"]["type"]?><br />
			<br />
			<a href="auth.html?logout=1">logout</a>
		<?php }		
	}else{
		switch ($_GET["mod"]){
			case "twitter":
				$tmhOAuth = new tmhOAuth($config["twitter"]);
				$here = tmhUtilities::php_self();
				function outputError($tmhOAuth) {
				  echo t('<h2>Beim Login über Twitter ist Fehler aufgetreten.</h2> <br /> Kannst du uns mitte einen Fehlerbericht mit dem unteren schicken?<br /><br /><pre>') . $tmhOAuth->response['response'] . PHP_EOL;
				  tmhUtilities::pr($tmhOAuth);
				  echo "</pre><br /> Danke!";
				}
				
				
				if ( isset($_SESSION['access_token']) ) {
				  $tmhOAuth->config['user_token']  = $_SESSION['access_token']['oauth_token'];
				  $tmhOAuth->config['user_secret'] = $_SESSION['access_token']['oauth_token_secret'];
				
				  $code = $tmhOAuth->request('GET', $tmhOAuth->url('1/account/verify_credentials'));
				  if ($code == 200) {
				    $resp = json_decode($tmhOAuth->response['response']);
				    echo t("Wilkommen ")." ".$resp->name;
				    $_SESSION["auth"]["type"] = "Twitter";
				    $_SESSION["auth"]["name"] = $resp->name;
				    $_SESSION["auth"]["id"]   = "twitter_".$resp->id;
				  } else {
				    outputError($tmhOAuth);
				  }
				// we're being called back by Twitter
				}elseif (isset($_REQUEST['oauth_verifier'])) {
				  $tmhOAuth->config['user_token']  = $_SESSION['oauth']['oauth_token'];
				  $tmhOAuth->config['user_secret'] = $_SESSION['oauth']['oauth_token_secret'];
				
				  $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', ''), array(
				    'oauth_verifier' => $_REQUEST['oauth_verifier']
				  ));
				
				  if ($code == 200) {
				    $_SESSION['access_token'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
				    unset($_SESSION['oauth']);
				    header("Location: {$here}");
				  } else {
				    outputError($tmhOAuth);
				  }
				// start the OAuth dance
				} else{
				  $callback = isset($_REQUEST['oob']) ? 'oob' : $here;
				
				  $params = array(
				    'oauth_callback'     => $callback
				  );
				
				  if (isset($_REQUEST['force_write'])) :
				    $params['x_auth_access_type'] = 'write';
				  elseif (isset($_REQUEST['force_read'])) :
				    $params['x_auth_access_type'] = 'read';
				  endif;
				
				  $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/request_token', ''), $params);
				
				  if ($code == 200) {
				    $_SESSION['oauth'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
				    $method = 'authenticate'; //isset($_REQUEST['authenticate']) ? 'authenticate' : 'authorize';
				    $force  = isset($_REQUEST['force']) ? '&force_login=1' : '';
				    $authurl = $tmhOAuth->url("oauth/{$method}", '') .  "?oauth_token={$_SESSION['oauth']['oauth_token']}{$force}";
					header("Location: $authurl");
					die();
					
				    echo '<p>To complete the OAuth flow follow this URL: <a href="'. $authurl . '">' . $authurl . '</a></p>';
				  } else {
				    outputError($tmhOAuth);
				  }
				}
				
				break;
				
			case "facebook":
				$facebook = new Facebook($config["facebook"]);
				
				if($facebook->getUser()) {
					$user = $facebook->api('/me');
					echo t("Wilkommen ")." ".$user["name"];
				    $_SESSION["auth"]["type"] = "Facebook";
				    $_SESSION["auth"]["name"] = $user["name"];
				    $_SESSION["auth"]["id"]   = "facebook_".$user["id"];	
				}else{
					header("Location: ".$facebook->getLoginUrl());
					die();
				}				
				break;
			case "mail":
					if ($_POST["mail"]){
						$empfaenger = $_POST["mail"];
	     				$betreff = 'Login mcpiraten.info';
	
	     				$url = "http://mcpiraten.info/auth-mail.html?confirm=".md5($_POST["mail"].$config["sec"]["mail"])."-".base64_encode($_POST["mail"]);
	     				$nachricht_text = t("Wikommen bei mcpiraten.info <br />öffne bitte den Link um dich anzumelden <br /><br />")." <a href='$url'>$url</a>";
	      
	     				$header  = 'From: "MCPiraten" <webserver@mcpiraten.info>' . "\r\n";
					    $header .= 'Reply-To: webserver@mcpiraten.info' . "\r\n";
					    $header .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
					    $header .= 'X-Mailer: PHP/' . phpversion();
					    
					    mail($empfaenger, $betreff, $nachricht_text, $header);		
					    echo t("Du bekommt gleich eine Mail mit der du dich anmelden kannst");
					}elseif ($_GET["confirm"]){
						$md5 	= explode("-",$_GET["confirm"]);
  						$mail 	= base64_decode($md5[1]);
  						$md5 	= $md5[0];
  						if (md5($mail.$config["sec"]["mail"]) == $md5){
  							$user = explode('@',$mail);
  							$user = $user[0];
  							echo t("Wilkommen ")." ".$user;
  							$_SESSION["auth"]["type"] = "eMail";
				    		$_SESSION["auth"]["name"] = $user;
				    		$_SESSION["auth"]["id"]   = "mail_".base64_encode($mail);
  						}else{
  							echo t("Der Link ist leider abgelaufen. Bitte lass dir einen neuen schicken");	
  						}
					}else{?>
						<form method=post>
							<?=t("Mail Adresse")?> : <input name=mail /><br />
							<input type=submit value='<?=t("senden")?>' />
						</form>
					<?php }
				break;
			default: echo t("<h2>Wie möchten Sie Sich Einloggen?</h2>");
		}
	}	
}// Cookies Aktiv